package com.qs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.exception.ServiceException;
import com.qs.mapper.StoreCommentMapper;
import com.qs.mapper.StoreMapper;
import com.qs.model.dto.CollectStoreDto;
import com.qs.model.entity.CollectStore;
import com.qs.mapper.CollectStoreMapper;
import com.qs.model.entity.Store;
import com.qs.model.entity.StoreComment;
import com.qs.response.ResponseEnum;
import com.qs.service.ICollectStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户关注店铺表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-02
 */
@Service
@RequiredArgsConstructor
public class CollectStoreServiceImpl extends ServiceImpl<CollectStoreMapper, CollectStore> implements ICollectStoreService {

    private final StoreMapper storeMapper;
    private final CollectStoreMapper collectStoreMapper;
    private final StoreCommentMapper storeCommentMapper;

    @Override
    public void handleCollectStore(CollectStore store) {
        QueryWrapper<CollectStore> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", store.getUserId());
        wrapper.eq("store_id",store.getStoreId());
        CollectStore collectStore = baseMapper.selectOne(wrapper);
        if(null != collectStore) {
            throw new ServiceException(ResponseEnum.HAVE_COLLECTED_STORE);
        }
        baseMapper.insert(store);
    }

    @Override
    public List<CollectStoreDto> queryCollectStore(Integer userId) {
        QueryWrapper<CollectStore> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<CollectStore> selectList = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(selectList)){
            List<CollectStoreDto> list = new ArrayList<>();
            List<Integer> storeIds = selectList.stream().map(item -> item.getStoreId()).collect(Collectors.toList());
            List<Store> stores = storeMapper.selectBatchIds(storeIds);
            stores.stream().forEach(store -> {
                Long collectNum = collectStoreMapper.selectCount(new QueryWrapper<CollectStore>().eq("store_id", store.getId()));
                Long commentNum = storeCommentMapper.selectCount(new QueryWrapper<StoreComment>().eq("store_id", store.getId()));
                CollectStoreDto collectStoreDto = new CollectStoreDto();
                collectStoreDto.setId(store.getId());
                collectStoreDto.setImage(store.getImage());
                collectStoreDto.setHomeName(store.getHomeName());
                collectStoreDto.setCollectNum(collectNum);
                collectStoreDto.setCommentNum(commentNum);
                list.add(collectStoreDto);
            });
            return list;
        }
        return null;
    }
}
